<?php

/**
 * 后台 文章栏目模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: category.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class categoryAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('category');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_editor');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        //TODO  取出所有资讯栏目
        $rs = $this->tmp_model->getCategory(0, 0);
        $rs = empty($rs) ? '<tr><td colspan="4">请先添加分类</td></tr>' : $rs;
        $this->assign('rs', $rs);
        $this->V('category');
    }

    /**
     * 新增/修改栏目页面
     */
    public function add()
    {
        $cat_id = empty($_GET['cat_id']) ? 0 : (int) $_GET['cat_id'];
        $button = '添加分类';
        if ($cat_id > 0) {
            $editinfo = $this->tmp_model->getCategoryInfo($cat_id);
            $button = '保存修改';
        } else {
            $editinfo = array('cat_id' => '', 'channeltype' => '', 'cat_name' => '', 'category_keywords'=>'', 'category_description'=>'', 'category_nicename' => '', 'category_content' => '', 'cat_order' => '0', 'cat_pid' => '');
        }
        //栏目模型分类
        $channeltype = Tmac::config('channel.channeltype');
        $channeltype_option = Utility::Option($channeltype, $editinfo['channeltype']);

        $treers = $this->tmp_model->tree(0, 0, $editinfo['cat_pid']);
        $this->assign('editinfo', $editinfo);
        $this->assign('treers', $treers);
        $this->assign('cat_id', $cat_id);
        $this->assign('button', $button);
        $this->assign('channeltype_option', $channeltype_option);
        $this->V('category');
    }

    /**
     * 新增/修改栏目页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        $cat_id = empty($_POST['cat_id']) ? 0 : (int) $_POST['cat_id'];
        $cat_pid = intval($this->getParam('cat_pid'));
        if (!empty($cat_id) &&($cat_id == $cat_pid)) {
            $this->redirect('栏目父级栏目不能是自己！');
            exit;
        }
        if (empty($_POST['cat_name'])) {
            $this->redirect('请填写标题！');
            exit;
        }
        $category_keywords = $this->H($this->getParam('category_keywords'));
        $category_description = $this->H($this->getParam('category_description'));

        $info = array(
            'cat_name' => $_POST['cat_name'],
            'channeltype' => intval($this->getParam('channeltype')),
            'category_nicename' => $_POST['category_nicename'],
            'category_keywords' => $category_keywords,
            'category_description' => $category_description,
            'cat_pid' => $_POST['cat_pid'],
            'category_content' => $this->getParam('category_content'),
            'cat_order' => $_POST['cat_order']
        );

        if ($cat_id > 0) {
            //update save article_class
            $where = 'cat_id = ' . $cat_id . '';
            $rs = $this->tmp_model->addClass($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改新分类成功', PHP_SELF . '?m=category');
            } else {
                $this->redirect('修改新分类失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addClass($info);
            if ($rs) {
                $this->redirect('添加新分类成功', PHP_SELF . '?m=category');
            } else {
                $this->redirect('添加新分类失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $cat_id = empty($_GET['cat_id']) ? 0 : (int) $_GET['cat_id'];
        $rs = $this->tmp_model->delClass($cat_id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $this->redirect('删除分类成功', PHP_SELF . '?m=category');
        } else {
            $this->redirect('删除分类失败，请重试！');
        }
    }


}